class CulturalHeritage::GroupsController < ApplicationController
  #before_filter :authenticate_user!
  # GET /cultural_heritage/groups
  # GET /cultural_heritage/groups.xml
  def index
    @title_view = 'Grupos'
    @cultural_heritage_groups = CulturalHeritage::Group.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @cultural_heritage_groups }
    end
  end

  # GET /cultural_heritage/groups/1
  # GET /cultural_heritage/groups/1.xml
  def show
    @cultural_heritage_group = CulturalHeritage::Group.find(params[:id])
    @title_view = 'Grupos'
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @cultural_heritage_group }
    end
  end

  # GET /cultural_heritage/groups/new
  # GET /cultural_heritage/groups/new.xml
  def new
    if (user_signed_in? && ([2].include?(current_user.role)))
      @cultural_heritage_group = CulturalHeritage::Group.new
      @title_view = 'Grupos'
      respond_to do |format|
        format.html # new.html.erb
        format.xml  { render :xml => @cultural_heritage_group }
      end
    else
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end

  # GET /cultural_heritage/groups/1/edit
  def edit
    if (user_signed_in? && ([2].include?(current_user.role)))
    @title_view = 'Grupos'
    @cultural_heritage_group = CulturalHeritage::Group.find(params[:id])
    else
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end

  # POST /cultural_heritage/groups
  # POST /cultural_heritage/groups.xml
  def create
    if (user_signed_in? && ([2].include?(current_user.role)))
    @cultural_heritage_group = CulturalHeritage::Group.new(params[:cultural_heritage_group])
    @title_view = 'Grupos'
    respond_to do |format|
      if @cultural_heritage_group.save
        format.html { redirect_to(@cultural_heritage_group, :notice => 'Se ha creado correctamente el grupo.') }
        format.xml  { render :xml => @cultural_heritage_group, :status => :created, :location => @cultural_heritage_group }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @cultural_heritage_group.errors, :status => :unprocessable_entity }
      end
    end
    else
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end

  # PUT /cultural_heritage/groups/1
  # PUT /cultural_heritage/groups/1.xml
  def update
    if (user_signed_in? && ([2].include?(current_user.role)))
    @cultural_heritage_group = CulturalHeritage::Group.find(params[:id])
    @title_view = 'Grupos'
    respond_to do |format|
      if @cultural_heritage_group.update_attributes(params[:cultural_heritage_group])
        format.html { redirect_to(@cultural_heritage_group, :notice => 'Se ha actualizado correctamente el grupo.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @cultural_heritage_group.errors, :status => :unprocessable_entity }
      end
    end
    else
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end

  # DELETE /cultural_heritage/groups/1
  # DELETE /cultural_heritage/groups/1.xml
  def destroy
    if (user_signed_in? && ([2].include?(current_user.role)))
    @cultural_heritage_group = CulturalHeritage::Group.find(params[:id])
   mensaje = @cultural_heritage_group.erasable
    if mensaje.blank?
      @cultural_heritage_group.deleted = 1
      @cultural_heritage_group.save
    end
    respond_to do |format|
      format.html { redirect_to(cultural_heritage_groups_url, :notice => mensaje) }
      format.xml  { head :ok }
    end
    else
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end
end
